package kotlincode

import kotlinx.coroutines.*
import kotlin.coroutines.EmptyCoroutineContext

fun main() {
//    test8()
//    test9()
    println(f1.invoke(10))
}

fun test10(){
    GlobalScope.launch {

    }
    //实际是这样的：
    GlobalScope.launch(
        context = EmptyCoroutineContext,
        start = CoroutineStart.DEFAULT,
        block = CoroutineScope::func5
    )
}

val func5 :suspend CoroutineScope.() -> Unit = CoroutineScope::func5

suspend fun CoroutineScope.func5() : Unit {

}

//参考func3
val f4 : suspend CoroutineScope.(Int) -> Double = CoroutineScope::func4

suspend fun CoroutineScope.func4(num: Int) : Double {
    return num.toDouble()
}

//参考func1，只是多了一个suspend修饰符
val f3 :suspend (Int) -> Double = ::func3

suspend fun func3(num: Int) : Double {
    return num.toDouble()
}

fun CoroutineScope.func2(num: Int) : Double {
    return num.toDouble()
}

val f2 : CoroutineScope.(Int) -> Double = CoroutineScope::func2

fun func1(num : Int): Double {
    return num.toDouble().plus(10)
}

val f1 : (Int) -> Double = ::func1

fun test9() {
    val job:Job = GlobalScope.launch {                // 1
        println("Coroutine started!")   // 2
        delay(1000L)                    // 3
        println("Hello World!")         // 4
    }

    println("After launch!")            // 5
    Thread.sleep(2000L)                 // 6
    println("Process end!")             // 7
}

// 仅用于研究，生产环境不建议使用GlobalScope
fun test8() {
    // ①
    GlobalScope.launch {
        // ②
        delay(1000L)
        println("Hello World!")
    }

    // ③
    Thread.sleep(1500L)
//    Thread.sleep(500L)
}